var ConcertTypes = {
    invalidType: null,
    validType: null,
    parrentContainer: null,
    parrentJudgeContainer: null,
    show: function() {
        ConcertTypes.parrentContainer.style.display = "block";
        ConcertTypes.parrentJudgeContainer.style.display = "block";
        ConcertTypes.validType.checked = true;
    },
    hide: function() {
        ConcertTypes.parrentContainer.style.display = "none";
        ConcertTypes.parrentJudgeContainer.style.display = "none";
        ConcertTypes.invalidType.checked = true;
    },
    changeVisibility: function(fest_id) {
        if (fest_id != "") {
            xmlhttp.open("GET", "/ajax-helper/should-hide-concert/"+ fest_id +"/",true);
            xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState==4) {
                    if (xmlhttp.responseText == "hide")
                        ConcertTypes.hide()
                    else
                        ConcertTypes.show()
                }
            }
            xmlhttp.send(null)
        } else
            ConcertTypes.show();
    },
    onFestivalChanged: function(e) {
        ConcertTypes.changeVisibility(e.target.value);  
    },
    init: function() {
        ConcertTypes.invalidType = document.getElementById("id_concert_type_0");
        ConcertTypes.invalidType.parentNode.style.display = "none";
        ConcertTypes.validType = document.getElementById("id_concert_type_1");
        if (document.getElementById("id_concert_type_2").checked) {
            ConcertTypes.validType = document.getElementById("id_concert_type_2");
        }
        ConcertTypes.parrentContainer = ConcertTypes.validType.parentNode.parentNode.parentNode;
        
        var judgeEl = document.getElementById("id_judges");
        ConcertTypes.parrentJudgeContainer = judgeEl.parentNode;
        
        var festivalEl = document.getElementById("id_festival"); 
        ConcertTypes.changeVisibility(festivalEl.value);
        addEvent(festivalEl, 'change', ConcertTypes.onFestivalChanged);
    }
}

addEvent(window, 'load', ConcertTypes.init);